//
//  ReplayView.m
//  danpin
//
//  Created by chuangjia on 17/12/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "ReplayView.h"
#import "viewconfig.h"
#import "NSDictionary+ObjectExt.h"
#import "CalcLabel.h"
#import "TTTAttributedLabel.h"

@interface ReplayCell : UITableViewCell
{
}
@property(nonatomic,strong) id data;
@property(nonatomic,strong)TTTAttributedLabel *contentLabel;
@property(nonatomic,strong)UILabel *time;
@property(nonatomic,strong) UIView *bgView;

/// 根据数据模型来显示内容
- (void)showInfo:(id)model indexPath:(NSIndexPath *)indexPath;
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model;
@end

@implementation ReplayCell
@synthesize data=_data;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        _bgView =[[UIView alloc] initWithFrame:self.bounds];
        _bgView.backgroundColor=UIColorFromRGB(0xf7f7f7);
        [self.contentView addSubview:_bgView];
        
        _time =[[UILabel alloc] initWithFrame:CGRectZero];
        _time.backgroundColor = [UIColor clearColor];
        _time.textColor = CONTEXT_COLOR;
        _time.textAlignment = NSTextAlignmentLeft;
        _time.font = FONT(13);
        _time.numberOfLines=0;
        [_bgView addSubview:_time];
        
        _contentLabel =[[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*6,0)];
        _contentLabel.backgroundColor = [UIColor clearColor];
        _contentLabel.textColor =  CONTEXT_COLOR;
        _contentLabel.textAlignment = NSTextAlignmentLeft;
        _contentLabel.font = FONT(13);
        _contentLabel.lineBreakMode = NSLineBreakByWordWrapping|NSLineBreakByTruncatingTail;
        _contentLabel.numberOfLines=0;
        [_bgView addSubview:_contentLabel];
        
    }
    return self;
}

-(void)dealloc
{
    
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    _bgView.frame =CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, self.bounds.size.height - PHOTO_FRAME_WIDTH);
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model indexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    //
     NSString *nick_name =[dict stringForKey:@"nick_name" withDefaultValue:@""];
    if (nick_name.length>0) {
        _time.text =nick_name;
        _time.frame =CGRectMake(PHOTO_TIME_PADDING,PHOTO_TIME_PADDING,self.frame.size.width-PHOTO_FRAME_WIDTH, 20);
    }
   
    NSString *content =[dict stringForKey:@"text" withDefaultValue:@""];
    if (content.length>0) {
         content =[NSString stringWithFormat:@"“%@”",content];
        [_contentLabel setText:content afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString  *(NSMutableAttributedString *attributedString)
         {
             [attributedString addAttribute:NSFontAttributeName
                                      value:FONT(13)
                                      range:NSMakeRange(0,[attributedString length])];
             //设置字体颜色
             [attributedString addAttribute:(id)kCTForegroundColorAttributeName value:TITLE_COLOR range:NSMakeRange(0,[attributedString length])];
             
             CGFloat lineSpace = 3.0f;
             CTParagraphStyleSetting lineSpaceStyle;
             lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
             lineSpaceStyle.valueSize = sizeof(lineSpace);
             lineSpaceStyle.value =&lineSpace;
             
             //创建设置数组
             CTParagraphStyleSetting settings[ ] ={lineSpaceStyle};
             CTParagraphStyleRef style = CTParagraphStyleCreate(settings ,1);
             
             //给文本添加设置
             [attributedString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)CFBridgingRelease(style) range:NSMakeRange(0 , [attributedString length])];
             return attributedString;
             
         }];
        
        float fheight = [CalcLabel getAttributedStringHeightWidthValue:(DEVICE_WIDTH-PHOTO_FRAME_WIDTH*8) str:_contentLabel.attributedText];
        _contentLabel.frame =CGRectMake(PHOTO_TIME_PADDING, _time.frame.origin.y+_time.frame.size.height,self.frame.size.width-PHOTO_FRAME_WIDTH, fheight);
    }
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    NSDictionary *dict =(NSDictionary *)model;
    float hight =20;
    NSString *text =[dict stringForKey:@"text" withDefaultValue:@""];
    if (text.length>0) {
        text =[NSString stringWithFormat:@"“%@”",text];
        
        NSMutableAttributedString* attributedString =[[NSMutableAttributedString alloc]initWithString:text];
        [attributedString addAttribute:NSFontAttributeName value:FONT(13) range:NSMakeRange(0,[attributedString length])];
        
        CGFloat lineSpace = 3.0f;
        CTParagraphStyleSetting lineSpaceStyle;
        lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
        lineSpaceStyle.valueSize = sizeof(lineSpace);
        lineSpaceStyle.value =&lineSpace;
        
        //创建设置数组
        CTParagraphStyleSetting settings[ ] ={lineSpaceStyle};
        CTParagraphStyleRef style = CTParagraphStyleCreate(settings ,1);
        
        //给文本添加设置
        [attributedString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)CFBridgingRelease(style) range:NSMakeRange(0 , [attributedString length])];
        float fheight = [CalcLabel getAttributedStringHeightWidthValue:(DEVICE_WIDTH- PHOTO_FRAME_WIDTH*8) str:attributedString];
        if (fheight<20)
        {
            fheight=20;
        }
        attributedString = nil;
        hight =hight + fheight;
    }
    return hight +PHOTO_FRAME_WIDTH*2;
}
@end

@interface ReplayView()<UITableViewDataSource,UITableViewDelegate>
{
    
}
@property(nonatomic,strong)UITableView *tbView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@end
@implementation ReplayView
@synthesize tbView = _tbView;
@synthesize dataArray=_dataArray;
-(id)initWithFrame:(CGRect)frame dataDict:(NSArray*)data
{
    if(self = [super initWithFrame:frame])
    {
        self.backgroundColor = UIColorFromRGB(0xffffff);
        [self initUI];
        self.dataArray = [NSMutableArray arrayWithArray:data];
    }
    return self;
    
}
-(void)dealloc
{
    [self.dataArray removeAllObjects];
    self.dataArray  =nil;
    self.tbView =nil;
}
-(void)initUI
{
    _tbView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
    _tbView.backgroundColor= [UIColor clearColor];
    _tbView.dataSource = self;
    _tbView.delegate = self;
    _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tbView.showsVerticalScrollIndicator = NO;
    _tbView.scrollEnabled = NO;
    _tbView.scrollsToTop = NO;
    [self addSubview:_tbView];
}
#pragma mark - UITableView Datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     ReplayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TabMenuItem"];//之前为什么不重用cell
    if(cell == nil)
    {
        cell = [[ReplayCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TabMenuItem"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor =[UIColor clearColor];
        
    }
    if (indexPath.row >= self.dataArray.count) {
        return cell;
    }
    NSDictionary *dict =[self.dataArray objectAtIndex:indexPath.row];
    [cell showInfo:dict indexPath:indexPath];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row >= self.dataArray.count) {
        return 0;
    }
    NSDictionary *dict =[self.dataArray objectAtIndex:indexPath.row];
    return [ReplayCell returnCellHeight:dict];

}
@end
